package Veterinaria.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Veterinaria.excepcion.DAOExcepcion;
import Veterinaria.modelo.FacturaDetalleTMP;
import Veterinaria.negocio.GestionFacturaDetalle;

/**
 * Servlet implementation class EliminarDetalleFacturaServlet
 */
@WebServlet("/EliminarDetalleFacturaServlet")
public class EliminarDetalleFacturaServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EliminarDetalleFacturaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		

		String  id = request.getParameter("id");
		
		String  idfactura = request.getParameter("idfactura");
		String  idNomCliente = request.getParameter("idNomCliente");
		String  NomCliente = request.getParameter("NomCliente");

		String  fechafactura = request.getParameter("fechafactura");
		String  total = request.getParameter("total");
		double total1 =0;
		System.out.println(NomCliente);
		
		PrintWriter out;
		out = response.getWriter();
		
		GestionFacturaDetalle facdet = new GestionFacturaDetalle();
		
		System.out.println(id);
		
		try {
			facdet.eliminarfacturadetalleTemporal(id);
	
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		GestionFacturaDetalle fact = new GestionFacturaDetalle();
		try {
			request.setAttribute("facturas", fact.listartemporal());
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			GestionFacturaDetalle fat = new GestionFacturaDetalle();
			request.setAttribute("facturas", fact.listartemporal());
			Collection<FacturaDetalleTMP> o = null;
			try {
				o = fat.listartemporal();
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (o != null) {
			
				for (FacturaDetalleTMP x : o) {
					total1 = total1 + (x.getTotal());
				}
			}
			String simporte = String.valueOf(total1);
			request.setAttribute("var5", simporte);
			
			System.out.println(simporte);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		
		request.setAttribute("var2", idNomCliente);
		request.setAttribute("var3", NomCliente);
		request.setAttribute("var4", fechafactura);
//		request.setAttribute("var5", total);
		
			RequestDispatcher  rd= request.getRequestDispatcher("factura.jsp");
			rd.forward(request, response);			
	
		}
		
		
	}


